//
//  SYMainViewController.swift
//  SingWeibo
//
//  Created by shiying on 15/12/15.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit

class SYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //自定义tabbar
        let mainTabBar = SYMainTabbar()
        mainTabBar.gotoComposerViewController = {
        
            if SYUseAccount.userLogin {
            
                let composeVC = SYComposeViewController()
                let nav = UINavigationController(rootViewController: composeVC)
                self.presentViewController(nav, animated: true, completion: nil)
            
            } else {
                let oAuthVC = SYOauthViewController()
                self.presentViewController(UINavigationController(rootViewController: oAuthVC), animated: true, completion: nil)
            }
        }
        
        setValue(mainTabBar, forKey: "tabBar")
     
        ///首页
        let homeVC = SYHomeViewController()
        self.addchildVC(homeVC, title: "首页", imageName: "tabbar_home")
        ///消息
        let messageVC = SYMessageViewController()
        self.addchildVC(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        ///搜索
        let discoveryVC = SYDiscoveryViewController()
        self.addchildVC(discoveryVC, title: "发现", imageName: "tabbar_discover")
        
        ///我
        let profileVC = SYProfileViewController()
        self.addchildVC(profileVC, title: "我", imageName: "tabbar_profile")
        
        
    }
    
    /**
     *  controller 控制器
     *  title 标题
     *  imageName 图片的名字
     */
    private func addchildVC(controller: UIViewController, title: String, imageName: String) {
        
        //添加导航拦控制器
        self.addChildViewController(UINavigationController(rootViewController: controller))
        
        //设置标题
        controller.title = title
        
        //设置默认图片
        controller.tabBarItem.image = UIImage(named: imageName)
        
//        tabbar_compose_button_highlighted
        let highlighted = imageName + "_highlighted"
        //设置选中图片
        controller.tabBarItem.selectedImage = UIImage(named: highlighted)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置文字的颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
    }
}
